var cc__decorate = __decorate;
Object.defineProperty(exports, "__esModule", {
  value: true
});
var $2KDefine = require("KDefine");
var $2StateComponent = require("StateComponent");
var $2BulletMgr = require("BulletMgr");
var $2PlantMgr = require("PlantMgr");
var $2BundleManager = require("BundleManager");
var $2ExcelManager = require("ExcelManager");
var $2ZombieMgr = require("ZombieMgr");
var $2SceneStage = require("SceneStage");
var $2UserDataManager = require("UserDataManager");
var $2AudioManager = require("AudioManager");
var $2kc = require("kc");
var cc__decorator = cc._decorator;
var ccp_ccclass = cc__decorator.ccclass;
var ccp_property = cc__decorator.property;
var def_PlantBase = /* */function (_s$default3) {
  babel_inherits(y, _s$default3);
  var _super72 = babel_createSuper(y);
  function y() {
    var _this157;
    babel_classCallCheck(this, y);
    _this157 = _super72.apply(this, arguments);
    _this157.dragon = null;
    _this157.id = 1;
    _this157.hp = 10;
    _this157.attackCD = .8;
    _this157.damage = 1;
    _this157.shootDelay = .4;
    _this157.cost = 0;
    _this157.dragonName = null;
    _this157.skinName = null;
    _this157.skillCD = 0;
    _this157.remainCD = 0;
    _this157.skillNums = 1;
    _this157.attackNums = 1;
    _this157.attackSpeed = 1;
    _this157.scale = 1;
    _this157.skillDamage = 1;
    _this157.skillbullet = 1;
    _this157.shootSound = "";
    _this157.hitSound = "";
    _this157.skillSound = "";
    _this157.shootSoundVolume = 1;
    _this157.hitSoundVolume = 1;
    _this157.skillSoundVolume = 1;
    _this157.inUse = false;
    _this157.canMove = false;
    _this157.isPause = false;
    _this157.shootPo = cc.v2(29, 17);
    _this157.skillPos = cc.v2(29, 17);
    _this157.bulletId = 1;
    _this157.row = -1;
    _this157.col = -1;
    _this157.checkTargetTime = .2;
    _this157.pTime = 0;
    _this157.attackTimes = 0;
    return _this157;
  }
  babel_createClass(y, [{
    key: "onLoad",
    value: function onLoad() {
      this.regState($2KDefine.State.wait, this.stateWait);
      this.regState($2KDefine.State.waiting, this.stateWait);
      this.regState($2KDefine.State.attack, this.stateAttack);
      this.regState($2KDefine.State.attacking, this.stateAttack);
      this.regState($2KDefine.State.dead, this.stateDead);
      this.regState($2KDefine.State.deading, this.stateDead);
      this.node.getComponent(cc.BoxCollider).enabled = false;
    }
  }, {
    key: "reset",
    value: function reset(e) {
      var _this158 = this;
      var t = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
      this.dragon = this.node.getChildByName("p01").getComponent(sp.Skeleton);
      var a = $2ExcelManager.ExcelMgr.instance.GetExcelObject($2ExcelManager.TableName.plantTable)[e];
      this.inUse = false;
      this.attDistance = a.attDistance;
      this.skillSound = a.skillSound;
      this.shootSoundVolume = a.shootSoundVolume;
      this.hitSoundVolume = a.hitSoundVolume;
      this.skillSoundVolume = a.skillSoundVolume;
      this.pTime = 0;
      this.id = a.id;
      this.attackCD = a.attackCD;
      this.simpleDamage = a.simpleDamage;
      this.hitSound = a.hitSound;
      this.shootSound = a.shootSound;
      this.damage = a.damage[$2UserDataManager.default.instance.getPlantLevel(this.id) - 1][1] * this.simpleDamage;
      this.hp = a.hp;
      this.shootDelay = a.shootDelay;
      this.cost = a.cost[$2UserDataManager.default.instance.getPlantLevel(this.id) - 1][1];
      this.bulletId = a.bullet;
      this.dragonName = a.dragonName;
      this.skinName = a.skinName;
      this.skillCD = a.skillCD;
      this.remainCD = this.skillCD;
      this.shootPo = new cc.Vec2(a.shootPo[0], a.shootPo[1]);
      this.skillPos = new cc.Vec2(a.skillPos[0], a.skillPos[1]);
      this.attackNums = a.attackNums;
      this.skillNums = a.skillNums;
      this.attackSpeed = a.attackSpeed;
      this.skillDamage = a.skillDamage;
      this.skillbullet = a.skillbullet;
      this.node.getChildByName("p01").x = a.position[0];
      this.node.getChildByName("p01").y = a.position[1];
      if (!t) {
        if ($2BundleManager.default.instance.getBundleRes("Spine", $2PlantMgr.default.resPre + this.dragonName + "/" + this.dragonName, sp.SkeletonData)) {
          this.dragon.skeletonData = $2BundleManager.default.instance.getBundleRes("Spine", $2PlantMgr.default.resPre + this.dragonName + "/" + this.dragonName, sp.SkeletonData);
        } else {
          $2BundleManager.default.instance.loadBundleRes("Spine", $2PlantMgr.default.resPre + this.dragonName + "/" + this.dragonName, sp.SkeletonData, function () {
            _this158.dragon.skeletonData = $2BundleManager.default.instance.getBundleRes("Spine", $2PlantMgr.default.resPre + _this158.dragonName + "/" + _this158.dragonName, sp.SkeletonData);
          });
        }
      }
      a.skinName && this.dragon.setSkin(a.skinName);
      this.scale = a.scale;
    }
  }, {
    key: "play",
    value: function play(e, t) {
      var _this159 = this;
      this.inUse = true;
      this.row = e;
      this.col = t;
      this.node.zIndex = $2KDefine.ZIndex.plant - e;
      this.node.scale = this.scale;
      this.node.getComponent(cc.BoxCollider).enabled = true;
      if (0 != this.skillCD) {
        this.schedule(function () {
          _this159.remainCD--;
          _this159.remainCD < 0 && (_this159.remainCD = 0);
        }, 1);
      } else {
        this.remainCD = 1;
      }
      this.setState($2KDefine.State.wait);
    }
  }, {
    key: "removeBullet",
    value: function removeBullet(e) {
      e.inUse = false;
      cc.Tween.stopAllByTarget(e.node);
    }
  }, {
    key: "shoot",
    value: function shoot() {
      var _this160 = this;
      var e = this;
      var t = new cc.Vec2(this.node.getPosition().x + this.shootPo.x, this.node.getPosition().y + this.shootPo.y);
      if (0 == this.remainCD) {
        var _a40 = this.dragon.setAnimation(0, "dazhao", true);
        var _n43 = 0;
        this.dragon.setCompleteListener(function () {
          if (++_n43 == _this160.skillNums) {
            e.setState($2KDefine.State.wait);
            _this160.attackTimes += _this160.attackCD;
            _this160.remainCD = _this160.skillCD;
          }
        });
        this.dragon.setTrackEventListener(_a40, function (a, n) {
          if ("dazhao" == n.data.name) {
            $2AudioManager.default.instance.playEffect(_this160.skillSound, _this160.skillSoundVolume);
            $2BulletMgr.default.shoot_to_right($2SceneStage.default.instance.groudNode, t, e.bulletId, e.skillDamage * e.damage, e.attDistance, e.attackSpeed, _this160.hitSound, _this160.hitSoundVolume);
          }
        });
      } else {
        var _a41 = this.dragon.setAnimation(0, "gongji", true);
        var _n44 = 0;
        this.dragon.setCompleteListener(function () {
          ++_n44 == _this160.attackNums && e.setState($2KDefine.State.wait);
        });
        this.dragon.setTrackEventListener(_a41, function (a, n) {
          if ("gongji" == n.data.name) {
            $2AudioManager.default.instance.playEffect(_this160.shootSound, _this160.shootSoundVolume);
            $2BulletMgr.default.shoot_to_right($2SceneStage.default.instance.groudNode, t, e.bulletId, e.damage, e.attDistance, e.attackSpeed, _this160.hitSound, _this160.hitSoundVolume);
          }
        });
        this.attackTimes += this.attackCD;
      }
    }
  }, {
    key: "hurt",
    value: function hurt(e) {
      this.inUse && (this.hp <= 0 || (this.hp -= e, this.hp <= 0 && this.setState($2KDefine.State.dead)));
    }
  }, {
    key: "stop",
    value: function stop() {
      this.inUse = false;
      this.dragon.timeScale = 0;
      this.setState($2KDefine.State.idle);
    }
  }, {
    key: "stateAttack",
    value: function stateAttack(e) {
      if (this.state === $2KDefine.State.attack) {
        this.state = $2KDefine.State.attacking;
        this.shoot();
      }
    }
  }, {
    key: "stateDead",
    value: function stateDead(e) {
      var _this161 = this;
      if (this.state === $2KDefine.State.dead) {
        this.state = $2KDefine.State.deading;
        this.node.getChildByName("Effect").active = true;
        cc.tween(this.node.getChildByName("p01")).to(.5, {
          opacity: 0
        }).start();
        this.node.getChildByName("Effect").getComponent(sp.Skeleton).setAnimation(0, "newAnimation", false);
        this.node.getChildByName("Effect").getComponent(sp.Skeleton).setCompleteListener(function () {
          var e = _this161.node.getPosition();
          $2kc.default.log("植物死亡:", _this161.row, _this161.col, _this161.id, _this161.skinName);
          _this161.node.destroy();
          if (_this161.col < 5) {
            $2SceneStage.default.instance.showBombUI();
          } else {
            $2SceneStage.default.instance.showReviveUI(_this161, e);
          }
        });
        $2SceneStage.default.instance.delPlantInBlckArr(this.row, this.col);
      }
    }
  }, {
    key: "stateWait",
    value: function stateWait(e) {
      if (this.state === $2KDefine.State.wait) {
        this.state = $2KDefine.State.waiting;
        this.dragon.setAnimation(0, "daiji", true);
        this.dragon.setCompleteListener(null);
      }
      this.pTime += e;
      if (this.pTime > this.checkTargetTime && !this.isPause) {
        this.pTime = 0;
        this.attackTimes <= 0 && this.checkTarget() && this.setState($2KDefine.State.attack);
      }
    }
  }, {
    key: "checkTarget",
    value: function checkTarget() {
      var e = $2ZombieMgr.default.instance().zombieInRow[this.row];
      if (e.size <= 0) {
        return false;
      }
      var t = e.values();
      var a = t.next();
      for (; !a.done;) {
        var _e78 = a.value;
        if (_e78.hp > 0 && _e78.node.x > this.node.x) {
          return true;
        }
        a = t.next();
      }
      return false;
    }
  }, {
    key: "pause",
    value: function pause() {
      this.inUse = false;
      this.dragon.paused = true;
    }
  }, {
    key: "resume",
    value: function resume() {
      this.inUse = true;
      this.dragon.paused = false;
    }
  }, {
    key: "update",
    value: function update(e) {
      if (this.inUse) {
        this.attackTimes >= 0 && (this.attackTimes -= e);
        this.updateFrame(e);
      }
    }
  }]);
  return y;
}($2StateComponent.default);
def_PlantBase = cc__decorate([ccp_ccclass], def_PlantBase);
exports.default = def_PlantBase;